<template>
  <div class="table-page">
    <GiTable row-key="id" title="分类管理" :data="dataList" :columns="columns" :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }" :pagination="false" :disabled-tools="['size']"
      :disabled-column-keys="['name']" @refresh="search">
      <template #expand-icon="{ expanded }">
        <IconDown v-if="expanded" />
        <IconRight v-else />
      </template>
      <template #toolbar-left>
        <a-input v-model="queryForm.name" placeholder="请输入分类名称" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #status="{ record }">
        <GiCellStatus :status="record.status" />
      </template>
      <template #toolbar-right>
        <a-button v-permission="['category:category:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['category:category:update']" title="修改" @click="onUpdate(record)">修改</a-link>
          <a-link v-permission="['category:category:add']" @click="onAdd(record.id)">新增</a-link>
          <a-link v-permission="['category:category:delete']" status="danger" :disabled="record.disabled" title="删除"
            @click="onDelete(record)">
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <CategoryAddModal ref="CategoryAddModalRef" @save-success="search" />
    <CategoryDetailDrawer ref="CategoryDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import { deleteCategory, listCategory, type CategoryQuery, type CategoryResp } from '@/apis/website'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import CategoryAddModal from './CategoryAddModal.vue'
import CategoryDetailDrawer from './CategoryDetailDrawer.vue'

defineOptions({ name: 'CategoryManagement' })


const queryForm = reactive<CategoryQuery>({})

const {
  tableData: dataList,
  loading,
  search,
  handleDelete
} = useTable((page) => listCategory({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  { title: '名称', dataIndex: 'name', minWidth: 170, ellipsis: true, tooltip: true },
  { title: '编码', dataIndex: 'code'},
  { title: '状态', slotName: 'status', align: 'center' },
  { title: '排序', dataIndex: 'sort', align: 'center', show: true },
  { title: '描述', dataIndex: 'description', ellipsis: true, tooltip: true },
  { title: '创建人', dataIndex: 'createUserString', ellipsis: true, tooltip: true, show: false },
  { title: '创建时间', dataIndex: 'createTime', width: 180 },
  { title: '修改人', dataIndex: 'updateUserString', ellipsis: true, tooltip: true, show: false },
  { title: '修改时间', dataIndex: 'updateTime', width: 180, show: false },
  {
    title: '操作',
    slotName: 'action',
    width: 180,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['category:category:update', 'category:category:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.name = undefined
  queryForm.status = undefined
  search()
}

// 删除
const onDelete = (record: CategoryResp) => {
  return handleDelete(() => deleteCategory(record.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

const CategoryAddModalRef = ref<InstanceType<typeof CategoryAddModal>>()
// 新增
const onAdd = (parentId?: string) => {
  CategoryAddModalRef.value?.onAdd(parentId)
}

// 修改
const onUpdate = (record: CategoryResp) => {
  CategoryAddModalRef.value?.onUpdate(record.id)
}

const CategoryDetailDrawerRef = ref<InstanceType<typeof CategoryDetailDrawer>>()
// 详情
const onDetail = (record: CategoryResp) => {
  CategoryDetailDrawerRef.value?.onDetail(record.id)
}
</script>

<style lang="scss" scoped></style>
